<?php

class Items_model extends Model {
	
	
	var $item_id;
	var $pawn_id;			    // lookup field
	var $item_type_id;	    // lookup field
	var $item_description;
	var $metal_id;			    // lookup field
	var $item_kt_id;		    // lookup field
	var $wt;
	var $item_gender_id;	    // lookup field
	var $item_style_id;		 // lookup field
	var $size_length;
	var $number_of_stones;
	var $stone_shape_id;	    // loopup field
	var $stone_carat;
	var $stone_color_id;	    // lookup field
	var $item_note;			
		
	function Items_model()
	{
		parent::Model();
	}
	
	// This function compares an array of data to the table fields, and deletes any items in the data
	// that are not a field in the table
	
	protected function filterInputArray($table, $data, $xss_clean = false, $caps = true){
        $fields = $this->db->list_fields($table);

        foreach ($data as $k => $v) {
                if(in_array($k, $fields) == false){
                        unset($data[$k]);
                } 
				else 
				{
                        if($xss_clean === true) $data[$k] = $this->input->xss_clean($v);
                        if($caps === true) $data[$k] = strtoupper($v); 	// convert to CAPS
                }
        }

        return $data;
	}

	function general()
	{
		
		// Set up the parameters for the form_helper function that displays the fields 
		// in the items add and items update view
      $data['fPawnId']			  = array('name'=>'pawn_id','size'=>5);
      $data['fItemDescription'] = array('name'=>'item_description','size'=>25 ); 
      $data['fWt']			 	  = array('name'=>'wt','size'=>5 ); 
      $data['fSizeLength'] 	  = array('name'=>'size_length', 'size'=>5 ); 
      $data['fStones'] 			  = array('name'=>'number_of_stones', 'size'=>2 ); 	                                
      $data['fStoneCarat']      = array('name'=>'stone_carat', 'size'=>2 );  
      $data['fNote']  			  = array('name'=>'item_note', 'rows'=>2,'cols'=>50);                           
		
	    
	    // Get the options to poppulate the dropdown lookup fields
	    //$fPawnIdOptions = $this->get_Pawns();
	    $fItemTypesOptions = $this->get_ItemTypes();
	    $fMetalOptions = $this->get_Metals();
	    $fItemKtOptions = $this->get_ItemKts();
	    $fItemGenderOptions = $this->get_ItemGenders();
	    $fItemStyleOptions = $this->get_ItemStyles();
	    $fStoneShapeOptions = $this->get_StoneShapes();
	    $fStoneColorOptions = $this->get_StoneColors();
	    
	    // setup the lookup fields
	    //$data['fPawnId']		= array('name'=>'pawn_id', 'options' => $fPawnIdOptions, 'select' =>'');
	    $data['fItemType']		= array('name'=>'item_type_id', 'options' => $fItemTypesOptions, 'select' =>'');
	    $data['fMetalId']		= array('name'=>'metal_id', 'options' => $fMetalOptions, 'select' =>'');
	    $data['fItemKt']		= array('name'=>'item_kt_id', 'options' => $fItemKtOptions, 'select' => '');
	    $data['fItemGender']	= array('name'=>'item_gender_id', 'options' => $fItemGenderOptions, 'select' => '');
	    $data['fItemStyle']		= array('name'=>'item_style_id', 'options' => $fItemStyleOptions, 'select' => '');
	    $data['fStoneShape']	= array('name'=>'stone_shape_id', 'options' => $fStoneShapeOptions, 'select' => '');
	    $data['fStoneColor']	= array('name'=>'stone_color_id', 'options' => $fStoneColorOptions, 'select' => '');
	    
	    // Retrieve the database fields
	    $fields = $this->db->list_fields('items');
	    
	    // Create an associative array of the table field names as key, and an empty value
	    foreach ($fields as $k => $v)
	    	$empty_table[$v] = '';			
				
		// convert the array to objects and store it in $data['result']
		$data['result'] = parseArrayToObject($empty_table);	
		
		return $data;    	    
	}
	
	function get() {
		
		// If any of the following properties are set before Items_model->get() is called from the controller
		//  then we will include a where statement for each of the properties that have been set.
		
		if ($this->item_id) {
            $this->db->where("item_id", $this->item_id);
        }
        if ($this->pawn_id) {
            $this->db->where("items.pawn_id", $this->pawn_id);
        }
        
                
		$this->db->select('*');
		$this->db->select('pawns.pawn_id, item_types.item_type_description, metals.metal_description');
		$this->db->select('item_kts.item_kt, item_genders.item_gender_description');
		$this->db->select('item_styles.item_style_description, stone_shapes.stone_shape_description');
		$this->db->select('stone_colors.stone_color_description');

		$this->db->from('items');
		$this->db->join('pawns','items.pawn_id = pawns.pawn_id','left');
		$this->db->join('item_types','items.item_type_id = item_types.item_type_id','left');
		$this->db->join('metals','items.metal_id = metals.metal_id','left');
		$this->db->join('item_kts','items.item_kt_id = item_kts.item_kt_id','left');
		$this->db->join('item_genders','items.item_gender_id = item_genders.item_gender_id','left');
		$this->db->join('item_styles','items.item_style_id = item_styles.item_style_id','left');
		$this->db->join('stone_shapes','items.stone_shape_id = stone_shapes.stone_shape_id','left');
		$this->db->join('stone_colors','items.stone_color_id = stone_colors.stone_color_id','left');
		
		
		// We will display our results in order by last name and then first name.
        //$this->db->orderby("last_name, first_name");

		$query = $this->db->get();
		
		// If you set item_model->item_id from your controller, then there will only be one row to return.
        if ($this->item_id) {
            return ($query->row_array());		// returns an array
        }
        else
           	return $query->result_array();	// returns an array
	}
	
	function save($data)
	{
		// Allows you to add a new item as well as update an existing item
		$db_array = $this->filterInputArray('items', $data);
		
		// If Items_model->item_id was set in the controller, then we will update an existing record.
        if ($this->item_id) {
            $this->db->where("item_id", $this->item_id);
            $this->db->update("items", $db_array);
        }

        // If Items_model->item_id was not set in the controller, then we will insert a new record.
        else {
            $this->db->insert("items", $db_array);
        }
    }
	
	function delete($id)
	{
		// function deletes an item record based on the Id value
		// As long as Items_model->item_id was set in the controller, we will delete the record.
        
        $this->item_id = $id;
        
		if ($this->item_id) {
            $this->db->where("item_id", $this->item_id);
            $this->db->delete("items");
        }
		
	}
	
	// Lookup Functions
	
	function get_Pawns() {
			
		$query = $this->db->get('pawns');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['pawn_id'];
				$value = $row['pawn_control_number'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	function get_ItemTypes() {
			
		$query = $this->db->get('item_types');
		if ($query->num_rows() > 0) {		
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['item_type_id'];
				$value = $row['item_type_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_Metals() {
			
		$query = $this->db->get('metals');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['metal_id'];
				$value = $row['metal_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_ItemKts() {
			
		$query = $this->db->get('item_kts');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['item_kt_id'];
				$value = $row['item_kt'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_ItemGenders() {
			
		$query = $this->db->get('item_genders');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['item_gender_id'];
				$value = $row['item_gender_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_ItemStyles() {
			
		$query = $this->db->get('item_styles');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['item_style_id'];
				$value = $row['item_style_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	function get_StoneShapes() {
			
		$query = $this->db->get('stone_shapes');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['stone_shape_id'];
				$value = $row['stone_shape_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	function get_StoneColors() {
			
		$query = $this->db->get('stone_colors');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['stone_color_id'];
				$value = $row['stone_color_description'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}	
}

?>